/**
 * HibernateDao.java
 * 卢晓峰
 * Email：lxf9601@gmail.com
 * QQ：297051898
 * Created by 2010-7-18
 */
package com.autoabacus.core.config.persistence;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.autoabacus.core.config.PageData;

/**
 * Hibernage 持久化工具
 * @author 卢晓峰
 */
public class HibernateDao implements PersistenceProvider {
	
	private SessionFactory sessionFactory;
	
	private static final ThreadLocal<Session> sessionLocal = new ThreadLocal<Session>();
	
	private Session getSession() {
		Session session = (Session) sessionLocal.get();
		if(session != null && session.isOpen())
			return session;
		session = sessionFactory.openSession();
		sessionLocal.set(session);
		return session;
	}
	
	private void closeSession() {
		Session session = (Session)sessionLocal.get();
		if(session != null && session.isOpen())
			session.close();
	}
	
	@Resource
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/* (non-Javadoc)
	 * @see com.autoabacus.core.config.persistence.PersistenceProvider#executeUpdate(java.lang.String)
	 */
	@Override
	public int executeUpdate(String queryLanguage) {
		Session session = getSession();
		Query query = session.createQuery(queryLanguage);
		int efficeCount = query.executeUpdate();
		session.close();
		return efficeCount;
	}

	/* (non-Javadoc)
	 * @see com.autoabacus.core.config.persistence.PersistenceProvider#queryList(java.lang.String)
	 */
	@Override
	public List<Class<?>> queryList(String queryLanguage) {
		Session session = getSession();
		Query query = session.createQuery(queryLanguage);
		List<Class<?>> list = query.list();
		session.close();
		return list;
	}

	/* (non-Javadoc)
	 * @see com.autoabacus.core.config.persistence.PersistenceProvider#batchUpdate(java.lang.String[])
	 */
	@Override
	public int batchUpdate(String[] hqls) {
		Session session = getSession();
		Transaction transaction = session.beginTransaction();
		int effictCount = 0;
		for (String hql : hqls) {
			effictCount += session.createQuery(hql).executeUpdate();
		}
		transaction.commit();
		session.close();
		return effictCount;
	}

	/* (non-Javadoc)
	 * @see com.autoabacus.core.config.persistence.PersistenceProvider#queryPage(java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public PageData queryPage(String hql, int size, int page) {
		PageData pageData = new PageData();
		Session session = getSession();
		String countHql = "select count(*) " + hql.substring(hql.indexOf("from"));
		int totalRow = Integer.valueOf(session.createQuery(countHql).uniqueResult().toString());
		int totalPage = (totalRow - 1) / size + 1;
		int fromRow = (page - 1) * size;
		int toRow = page * size - 1; 
		if (page < 1) page = 1;
		if (page > totalPage) page = totalPage;
		pageData.setTatalRow(totalRow);
		pageData.setTotalPage(totalPage);
		pageData.setSize(size);
		pageData.setPage(page);
		pageData.setFromRow(fromRow);
		pageData.setToRow(toRow);
		
		List<Class<?>> dataList = session.createQuery(hql).setFirstResult(fromRow).setMaxResults(toRow).list();
		pageData.setDataList(dataList);
		
		return pageData;
	}

	/* (non-Javadoc)
	 * @see com.autoabacus.core.config.persistence.PersistenceProvider#executeInsert(com.autoabacus.core.config.provider.QuerySource)
	 */
	@Override
	public int executeInsert(String queryLanguage) {
		Session session = getSession();
		Query query = session.createSQLQuery(queryLanguage);
		int effectCount = query.executeUpdate();
		session.close();
		return effectCount;
	}

}
